<?php
/**
 * Introduction: 售后逻辑
 * @author: 杨陈鹏
 * @date: 2016/4/10 20:00
 * @email: yangchenpeng@cdlinglu.com
 */
namespace Common\Logic;

use Common\Model\AfterSalesModel;
use Common\Model\AfterSalesProblemTypeModel;

class AfterSalesLogic
{

    static public $model,$model_type;

    public function __construct()
    {

        self::$model = new AfterSalesModel();

        self::$model_type = new AfterSalesProblemTypeModel();

    }
    /**
     * Introduction: 添加售后问题
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function createProblem($data)
    {

        if(!$data['name'])
            return ['code' => 300, 'msg' => '请填写售后问题类型名称'];

        $datas['name'] = $data['name'];

        //return self::$model_type->fetchSql(true)->add($datas);

        if(self::$model_type->add($datas))
            return ['code' => 200, 'msg' => '添加成功'];


        return ['code' => 300, 'msg' => '添加失败'];

    }
    /**
     * Introduction: 售后问题列表
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function problemLists()
    {
        $data = self::$model_type->select();

        if(!$data)
            return ['code' => 300, 'msg' => '暂无售后问题类型'];

        return ['code' => 200, 'data' => $data];

    }

    /**
     * Introduction: 删除售后问题
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function problemDel($data)
    {

        if(!$data['id'])
            return ['code' => 300,'msg' => '缺少ID,无法删除'];

        if(self::$model_type->where(['id' => $data['id']])->delete())
            return ['code' => 200 ,'msg' => '删除成功'];


        return ['code' => 300 ,'msg' => '删除失败'];

    }


    /**
     * Introduction: 修改售后问题
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function problemUpdate($data)
    {

        if(!$data['id'])
            return ['code' => 300,'msg' => '参数错误!'];

        $id = $data['id'];
        unset($data['id']);

        if(!$data['name'])
            return ['code' => 300, 'msg' => '请填写售后问题类型名称'];


        if(self::$model_type->where(['id' => $id])->save(['name' => $data['name']]))
            return ['code' => 200 ,'msg' => '修改成功'];


        return ['code' => 300 ,'msg' => '修改失败'];

    }
    /**
     * Introduction: 申请售后
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */

    /*

    var data = {

        data:{

            'description'   : '描述',//必填且不能小于十个字符
            'order_id'      : '订单ID', //必须,订单ID
            'mobile'        : '手机号码',//必须
            'contacts'      : '联系人',//必须
            'problem_type_id'      : '问题类型ID',//可选
            'problem_type_name'      : '问题类型名称',//可选(有问题类型名称,必须有问题类型ID)
            'images'        : ['sdfas.jpg','sadfasdf,jpg'],//图片  可选

        },

    }

     */


    static public function create($data)
    {

        if(!$data['uid'])
            return ['code' => 500 ,'msg' => '请登录!'];

        if(strlen($data['description']) < 10)
            return ['code' => 300, 'msg' => '问题描述必须在十个字符以上'];

        if(!intval($data['order_id']))
            return ['code' => 300, 'msg' => '订单信息不能为空!'];

        if (!preg_match("/^1[3|4|5|6|7|8]\d{9}$/", $data['mobile']))
            return ['code' => 300, 'msg' => '手机号码格式错误!'];

        if(!$data['contacts'])
            return ['code' => 300, 'msg' => '请填写联系人!'];

        $datas['uid'] = $data['uid'];
        $datas['description'] = $data['description'];
        $datas['progress'] = 1;
        $datas['order_id'] = $data['order_id'];
        $datas['images'] = empty($data['images'])? '' : serialize($data['images']) ;
        $datas['create_time'] = NOW_TIME;
        $datas['problem_type_id'] = $data['problem_type_id'];
        $datas['problem_type_name'] = $data['problem_type_name'];
        $datas['mobile'] = $data['mobile'];
        $datas['contacts'] = $data['contacts'];
        $datas['money'] = intval(floatval($data['money']) * 100);


        if($id = self::$model->add($datas)){

            //如果申请成功,修改订单状态为待退款
            $order_logic = D('Common/Order','Logic');

            $result = $order_logic::updateStatus($datas['order_id'],5);

            if($result['code'] == 200){

                return ['code' => 200, 'msg' => '申请成功!'];

            } else {

                self::$model->where(['id' => $id])->delete();

                return ['code' => 300, 'msg' => '申请失败!'];
            }

        }

        return ['code' => 300, 'msg' => '申请失败!'];


    }
    /**
     * Introduction: 修改售后问题列表
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function update($data)
    {

        if(!$data['id'])
            return ['code' => 300 ,'msg' => '参数错误!'];

        $id = $data['id'];

        unset($data['id']);

        if(!$data)
            return ['code' => 300, 'msg' => '需要修改的数据不能为空!'];

        if(self::$model->where(['id' => $id])->save($data))
            return ['code' => 200 ,'msg' => '操作成功!'];


        return ['code' => 300, 'msg' => '操作成功!'];

    }

    /**
     * Introduction: 售后列表
     * @author: 杨陈鹏
     * @date: 2016/4/10 20:00
     * @email: yangchenpeng@cdlinglu.com
     * @param: array $data
     */
    static public function getData($where,$order = 'progress asc,create_time desc',$fields = '*',$page = 1,$nums = 20)
    {

        $total = self::$model
                        ->where($where)
                        ->count();

        $data = self::$model
                    //->fetchSql(true)
                    ->where($where)
                    ->field($fields)
                    ->page($page,$nums)
                    ->order($order)
                    ->select();

        $order_logic = D('Common/Order','Logic');

        foreach($data as $k => $v)
        {

            if($data[$k]['images'])
                $data[$k]['images'] = unserialize($data[$k]['images']);

            $order_info = $order_logic::$model
                                ->where(['id' => $v['order_id']])
                                ->field('number')
                                ->find();

            $data[$k]['order_info'] = $order_info;

        }

        if(!$total)
            return ['code' => 200, 'msg' => '暂无数据!'];

        return ['code' => 200,'total' => $total, 'data'=>$data];

    }

}